package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/**
 * Created by xlm on 2017/11/1 0001
 * 二维码扫描
 */

public class MyCaptureActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mLayoutBack;
    @Inject(R.id.title)
    private TextView mTextViewTitle;
    long timeStart;
    private Handler mHandler = new Handler();
    private String outTime;
    private String title;

    @Override
    protected void onGetIntent(Intent intent) {
        super.onGetIntent(intent);
        if (intent != null) {
            title = intent.getStringExtra("title");
        }
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_mycapture);
    }

    @Override
    protected void initViews() {
        mTextViewTitle.setText(String.valueOf(title));
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        /**
         * 执行扫面Fragment的初始化操作,替换扫描控件
         */
        final CaptureFragment captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        initFragmentManager(R.id.fl_my_container).showFragment(captureFragment);
//        getSupportFragmentManager().beginTransaction().add(R.id.fl_my_container, captureFragment).commit();
//        timeStart = System.currentTimeMillis() / 1000;
//        outTime = String.valueOf(PreferenceUtil.getParam(Preferences.OUTTIME, ""));
//        mHandler.postDelayed(mRunnable, 1000);
    }

//    /**
//     * 判断是否超时
//     */
//    private Runnable mRunnable = new Runnable() {
//        @Override
//        public void run() {
//            long timeDuration = System.currentTimeMillis() / 1000 - timeStart;
//            int duration = (int) timeDuration;
//            if (!CommonUtil.isEmpty(outTime)) {
//                if (duration >= Integer.valueOf(outTime)) {
//                    setBackFinish();
//                }
//            } else {
//                if (duration - (int) timeStart >= 20) {
//                    setBackFinish();
//                }
//            }
//            mHandler.postDelayed(this, 1000);
//        }
//    };
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
////        mHandler.removeCallbacks(mRunnable);
//    }

    @Override
    protected void onSetListener() {
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 返回
     */
    private void setBackFinish() {
        Intent resultIntent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
        bundle.putString(CodeUtils.RESULT_STRING, "");
        resultIntent.putExtras(bundle);
        setResult(RESULT_OK, resultIntent);
        finish();
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);
            setResult(RESULT_OK, resultIntent);
            finish();
        }

        @Override
        public void onAnalyzeFailed() {
            setBackFinish();
        }
    };

}
